<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Develop;

use Srv\Apps\Apis\Develop\Ctl\AdOpenCtl;
use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Proto\DTO\Develop\AdOpenAddDTO;
use Srv\Apps\Proto\DTO\Develop\AdOpenEditDTO;
use Srv\Apps\Proto\DTO\Develop\AdOpenListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Develop\AdOpenListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class AdOpenAct extends ActionAbstract
{
    /**
     * @url /app/open/list
     * @auth adOpenList
     * @req Srv.Apps.Proto.DTO.Develop.AdOpenListDTO
     * @rsp Srv.Apps.Proto.VO.Develop.AdOpenListVO
     * 开屏广告列表
     */
    public function adOpenList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof AdOpenListDTO) || !($VOClass instanceof AdOpenListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = AdOpenCtl::tag()->getAdOpenList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /app/open/add
     * @auth adOpenAdd
     * @req Srv.Apps.Proto.DTO.Develop.AdOpenAddDTO
     * 开屏广告新增
     */
    public function adOpenAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AdOpenAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData    = $RouteConfData->getParamsResData($DTOClass);
        $LogCtl     = LogCtl::tag();
        $logData    = $setData;
        $adopId     = AdOpenCtl::tag()->adOpenAdd($setData);
        if($adopId < 1){
            $errMsg = '开屏广告新增失败';
            $LogCtl->logAdd(LogCtl::AD_OPEN_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg     = '开屏广告新增成功';
        $LogCtl->logAdd(LogCtl::AD_OPEN_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto();
    }

    /**
     * @url /app/open/edit
     * @auth adOpenEdit
     * @req Srv.Apps.Proto.DTO.Develop.AdOpenEditDTO
     * 开屏广告编辑
     */
    public function adOpenEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof AdOpenEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $adopId         = intval($DTOClass->getAdopId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $AdOpenCtl      = AdOpenCtl::tag();
        $LogCtl         = LogCtl::tag();
        $adOpenSrc      = $AdOpenCtl->getAdOpenSrc($adopId);
        if(!isset($adOpenSrc['adop_id'])){
            $errMsg     = '开屏广告不存在';
            $logData    = $setData;
            $LogCtl->logAdd(LogCtl::AD_OPEN_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $result         = $AdOpenCtl->adOpenEdit($adopId, $setData);
        $logData        = ['new' => $setData, 'old' => $adOpenSrc];
        if(!$result){
            $errMsg     = '开屏广告编辑失败';
            $LogCtl->logAdd(LogCtl::AD_OPEN_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '开屏广告编辑成功';
        $LogCtl->logAdd(LogCtl::AD_OPEN_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto();
    }
}